﻿using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Tanmia.Basis.Prism;
using Tanmia.Basis.ViewInterface;
using Tanmia.Basis.ViewModel;
using Tanmia.Warehouse.Core;
using Tanmia.Warehouse.Domain;

namespace Tanmia.Warehouse.Presentation.Scenarios
{
    public class StoreStructureViewModel : BaseEditAndBrowseViewModel<Section>
    {
        public List<TreeViewItem> TreeItems { get; protected set; }

        protected SectionDomain SectionDomain
        {
            get { return DomainObject as SectionDomain; }
        }
        protected BranchDomain BranchDomain { get; set; }
        protected SectionTypeDomain SectionTypeDomain { get; set; }

        public ICommand ShowStorageSectionEditor { get; protected set; }

        public StoreStructureViewModel(IShellView shellView, IRegionManager regionManager, IMessageService messageService, SectionDomain storageSectionDomain, BranchDomain branchDomain, SectionTypeDomain sectionDomain)
            : base(shellView, regionManager, messageService, storageSectionDomain)
        {
            this.BranchDomain = branchDomain;
            this.SectionTypeDomain = sectionDomain;

            EntityName = "StorageSection";

            TreeItems = new List<TreeViewItem>();
            TreeItems.Add(new TreeViewItem { Header = "المؤسسة" });

            ShowStorageSectionEditor = new DelegateCommand<TreeViewItem>(OnShowStorageSectionEditor, OnCanShowStorageSectionEditor);
        }

        private bool OnCanShowStorageSectionEditor(TreeViewItem tvItem)
        {
            //return (tvItem != null);
            return true;
        }

        private void OnShowStorageSectionEditor(TreeViewItem tvItem)
        {
            if (tvItem.DataContext == this)
                RegionManager.RequestNavigate("StorageSectionRegion", "BranchEditor");
            else
                RegionManager.RequestNavigate("StorageSectionRegion", "SectionEditor", "selectedTreeNode", tvItem.DataContext);
        }


        protected override void OnRefresh()
        {
            PerformActualRefresh();
        }

        protected override void PerformActualRefresh()
        {
            base.PerformActualRefresh();
            BuildStorageSectionTree();
        }

        private void BuildStorageSectionTree()
        {
            TreeViewItem enterpriseItem = TreeItems[0];

            IList<Branch> branchs = BranchDomain.Gets();
            BranchDomain.CloseSession();

            foreach (Branch branch in branchs)
            {
                TreeViewItem tvItem = new TreeViewItem { Header = branch.Name, DataContext = branch };
                IList<Section> children = SectionDomain.GetParentSections(branch);
                PopulateStorageSectionItems(tvItem, children);
                enterpriseItem.Items.Add(tvItem);
            }
        }

        private void PopulateStorageSectionItems(TreeViewItem tvItem, IList<Section> children)
        {
            IList<Section> items = null;
            TreeViewItem treeItem = null;

            foreach (Section child in children)
            {
                items = SectionDomain.GetChildSections(child);
                if (items.Count > 0)
                {
                    treeItem = new TreeViewItem { Header = child.Name, DataContext = child };
                    PopulateStorageSectionItems(treeItem, items);
                    tvItem.Items.Add(treeItem);
                }
                else
                {
                    treeItem = new TreeViewItem { Header = child.Name };
                    tvItem.Items.Add(treeItem);
                }
            }
        }
    }
}
